package cn.net.isir.service;

import cn.net.isir.dao.MinipIndexDataDao;
import cn.net.isir.entity.Message;
import cn.net.isir.utils.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuxubo
 * @filename MinipIndexMessageService.java
 * @package cn.net.isir.service
 * @email lxb@isir.net.cn
 * @date 2019/03/22
 * @comment
 * @since 1.8
 */
@Service
public class MinipIndexMessageService {
    @Resource
    private MinipIndexDataDao minipIndexDataDao;

    /**
     * 获取最新的消息标题
     *
     * @return 消息
     */
    public Message getIndexMessage() {
        return minipIndexDataDao.msgTitel();
    }

    /**
     * 通知列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public JSONObject findMsgList(Integer pageNo, Integer pageSize) {
        List<Map> list = new ArrayList<>();
        PageInfo<Message> messagePageInfo = minipIndexDataDao.queryMsgList(pageNo, pageSize);
        List<Message> messageList = messagePageInfo.getList();
        Map<String, Object> map;
        for (Message message : messageList) {
            map = new HashMap<>();
            //获取标题title
            map.put("title", message.getTitle());
            //获取状态status(状态：0表示未读，1表示已读)
            map.put("status", message.getStatus());
            //获取发布者sender
            map.put("sender", message.getCreater());
            //获取发布时间sendTime
            map.put("sendTime", message.getPublishTime());
            //获取内容content
            map.put("content", message.getContent());
            //获取通知主键id
            map.put("id", message.getId());
            list.add(map);
        }
        JSONObject jo;
        jo = JSONUtils.toJSONObject(messagePageInfo);
        jo.put("list", list);
        return jo;
    }

    /**
     * 根据消息id获取消息详情
     *
     * @param id
     * @return
     */
    public Map findMsgInfo(String id) {
        Map<String, Object> map = new HashMap<>();
        Message msgInfo = minipIndexDataDao.findMsgInfo(id);
        //获取主键id
        map.put("id", msgInfo.getId());
        //获取标题title
        map.put("title", msgInfo.getTitle());
        //获取发送者sender
        map.put("sender", msgInfo.getCreater());
        //获取发送时间sendTime
        map.put("sendTime", msgInfo.getPublishTime());
        //获取内容content
        map.put("content", msgInfo.getContent());
        return map;
    }
}
